package io.virtualapp.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import io.virtualapp.VCommends;
import io.virtualapp.home.models.AppInfo;

/********************
 *  Created by ：  Zeoy 
 *         Date：  2018\3\27 0027.
 * Introduction：  获取app列表   根据包名获取appicon 的工具类
 *********************/

public class ApkTool {
    static String TAG = "ApkTool";

    public static List<AppInfo> scanLocalInstallAppList(PackageManager packageManager, Context context, boolean fastOpen) {
        List<AppInfo> myAppInfos = new ArrayList<AppInfo>();
        try {
            Intent intent = new Intent( Intent.ACTION_MAIN,null);
            intent.addCategory( Intent.CATEGORY_LAUNCHER);
            List<ResolveInfo> packageInfos = packageManager.queryIntentActivities(intent,0);
            if (packageInfos.size()>0){
                for (int i = 0; i < packageInfos.size(); i++) {
                    ResolveInfo packageInfo = packageInfos.get(i);
                    AppInfo info = new AppInfo();
                    //过滤掉系统app
                    PackageInfo packageInfo1 =packageManager.getPackageInfo(packageInfo.activityInfo.packageName,0);
//                    if ((ApplicationInfo.FLAG_SYSTEM & packageInfo1.applicationInfo.flags) != 0) {
//                        info._issystem=true;
//                    } else {
//                        info._issystem=false;
//                    }
                    info.packageName=packageInfo.activityInfo.packageName;
                    if (packageInfo.loadIcon(packageManager) == null
                            || packageInfo.loadLabel(packageManager) == null) {
                        continue;
                    }
                    //过滤自己
                    if (packageInfo.activityInfo.packageName.equals( VCommends.PACKAG_NAME))
                        continue;
                    info.name=packageInfo.loadLabel(packageManager).toString();
                    info.fastOpen = fastOpen;
                    info.path =getApkPath(context, info.packageName) ;
                    info.icon = getIcon(context,info.packageName);
                    info.name=packageInfo.loadLabel(packageManager).toString();
                    myAppInfos.add(info);
                }
            }
                 } catch (Exception e) {
            Log.e(TAG, "===============获取应用包信息失败");
        }
        return myAppInfos;
    }




    /**
     * 根据包名获取应用图标
     *
     * @param context
     * @param pakgename
     * @return
     */
    public static Drawable getIcon(Context context, String pakgename) {
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appInfo = pm.getApplicationInfo(pakgename, PackageManager.GET_META_DATA);
            Drawable appIcon = pm.getApplicationIcon(appInfo);
            return appIcon;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    //app名字
    public static String getLable(Context context, String pakaename){
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appInfo = pm.getApplicationInfo(pakaename, PackageManager.GET_META_DATA);
            String AppLable = pm.getApplicationLabel(appInfo).toString();
          // appInfo.sourceDir
            return AppLable;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    //app的apk路径
    public static String getApkPath(Context context,String pakaename){
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appInfo = pm.getApplicationInfo(pakaename, PackageManager.GET_META_DATA);
            String AppLable = pm.getApplicationLabel(appInfo).toString();
            String path=  appInfo.sourceDir;
            return path;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

//    /**
//     * 获取桌面的应用包名称
//     *
//     * @return
//     */
//    public static List<String> getHomes() {
//        List<String> names = new ArrayList<>();
//        PackageManager packageManager = MyApplication.getInstance().getPackageManager();
//        Intent intent = new Intent( Intent.ACTION_MAIN);
//        intent.addCategory( Intent.CATEGORY_HOME);
//        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
//        for (ResolveInfo r :
//                resolveInfos) {
//            names.add(r.activityInfo.packageName);
//        }
//        return names;
//    }

}